﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using org.smslib.modem;
using org.smslib.modem.athandler;
using System.Threading;

namespace SmsUI.SmsCore
{
    public class Q2385CModemGateway : IPModemGateway
    {
        public Q2385CModemGateway(string id, string ipName, int ipPort, string manufacturer, string model) :
            base(id, ipName, ipPort, manufacturer, model)
        {
            AATHandler at = new ATHandler_2583C(this);

            base.setAtHandler(at);
        }
        private DateTime preSendTime;
        public override bool sendMessage(org.smslib.OutboundMessage msg)
        {
            if (msg.getText() == SmsHelper.SmsHelper.IsCallNum)
            {
                bool re = false;
                lock (getModemDriver().getSYNCCommander())
                {
                    re = bool.Parse(sendCustomATCommand(msg.getRecipient()));
                    if (re)
                    {

                        msg.setMessageStatus(org.smslib.OutboundMessage.MessageStatuses.SENT);
                        msg.setFailureCause(org.smslib.OutboundMessage.FailureCauses.NO_ERROR);
                    }
                }
                return re;
            }

            if (getStatus() != GatewayStatuses.STARTED) return false;
            long timeLong = DateTime.Now.Ticks - preSendTime.Ticks;
            long sleepTime = 4000 - timeLong / 10000;
            if (sleepTime > 0)
            {
                Thread.Sleep((int)sleepTime);
                System.Diagnostics.Debug.WriteLine("sleep time" + sleepTime.ToString());
            }
            lock (getModemDriver().getSYNCCommander())
            {


                //if (msg.getText() == SmsHelper.IsCallNum)
                //{
                //    bool re = bool.Parse(getATHandler().sendCustomATCommand(msg.getRecipient()));
                //    if (re)
                //    {
                //        msg.setMessageStatus(org.smslib.OutboundMessage.MessageStatuses.SENT);
                //        msg.setFailureCause(org.smslib.OutboundMessage.FailureCauses.NO_ERROR);
                //    }
                //    return re;
                //}


                preSendTime = DateTime.Now;

                return sendMessagePDU(msg);
                //else if (getProtocol() == Protocols.TEXT) return sendMessageTEXT(msg);
            }
            return true;
        }


        private bool sendMessagePDU(org.smslib.OutboundMessage msg)
        {
            //string userData = "看地方都是";


            int refNo = getATHandler().sendMessage(1, msg.getText(), msg.getRecipient(), msg.getText());
            if (refNo >= 0)
            {
                msg.setGatewayId(getGatewayId());
                msg.setRefNo(refNo.ToString());
                DateTime now = DateTime.Now;
                java.util.Date date = new java.util.Date(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second);
                msg.setDispatchDate(date);
                msg.setMessageStatus(org.smslib.OutboundMessage.MessageStatuses.SENT);
                msg.setFailureCause(org.smslib.OutboundMessage.FailureCauses.NO_ERROR);
                incOutboundMessageCount();

            }
            else
            {
                msg.setGatewayId(getGatewayId());
                msg.setRefNo(null);
                DateTime now = DateTime.Now;
                java.util.Date date = new java.util.Date(now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second);
                msg.setDispatchDate(date);
                msg.setMessageStatus(org.smslib.OutboundMessage.MessageStatuses.FAILED);
                msg.setFailureCause(org.smslib.OutboundMessage.FailureCauses.UNKNOWN);

                //  msg.setErrorMessage(ge .getLastErrorText());
            }
            return true;
        }
    }
}
